﻿using System;
using Game2DFramework.Extensions;
using Game2DFramework.States;
using Game2DFramework.States.Transitions;
using MechGame.Game;
using MechGame.Utility;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MechGame.GameMenu
{
    class MainMenuState : InitializableState
    {
        public static readonly Type Id = typeof(MainMenuState);

        private Menu _menu;
        private Cursor _cursor;
        private SpriteFont _menuFont;

        protected override void OnEntered(object enterInformation)
        {
        }

        protected override void OnInitialize(object enterInformation)
        {
            _menu = new Menu(Game, MenuState.Shown);
            _menu.AppendMenuItem(new MenuItem("New Game", OnNewGame));
            _menu.AppendMenuItem(new MenuItem("Load Game", () => { }));
            _menu.AppendMenuItem(new MenuItem("Options", () => { }));
            _menu.AppendMenuItem(new MenuItem("Quit", OnQuit));
            _menuFont = Game.Content.Load<SpriteFont>("Fonts/DialogFont");

            _cursor = new Cursor(Game);
        }

        public override void OnLeave()
        {
            
        }

        private void OnQuit()
        {
            _stateChangeInformation = StateChangeInformation.QuitGameInformation(typeof (ThrowAwayTransition));
        }

        private StateChangeInformation _stateChangeInformation;

        private void OnNewGame()
        {
            _stateChangeInformation = StateChangeInformation.StateChange(InGameState.Id, typeof (SlideTransition));
        }

        public override StateChangeInformation OnUpdate(float elapsed)
        {
            _stateChangeInformation = StateChangeInformation.Empty;

            _menu.Update(elapsed);
            _cursor.Update();

            return _stateChangeInformation;
        }

        private const string VersionInfo = "Version 0.1 Alpha";

        public override void OnDraw(float elapsedTime)
        {
            var spriteBatch = Game.SpriteBatch;
            Game.GraphicsDevice.Clear(Color.Black);
            
            _menu.Draw();

            var size = _menuFont.MeasureString(VersionInfo);
            spriteBatch.DrawString(_menuFont, VersionInfo, (new Vector2(Game.ScreenWidth - 5, Game.ScreenHeight - 5) - size).SnapToPixels(), Color.White);

            _cursor.Draw();
        }
    }
}
